#!/usr/bin/env python
import sys
import helper
import class_set
from sets import Set

class CommonRecommendationReducer(helper.ReducerHelper):
    # the result is a tuple
    res = []
    
    def tuple_cmp(self, left, right):
        assert(len(left) == 2 and len(right) == 2)
        return -cmp(left[1], right[1])

    def summary(self):
        recom_result = class_set.RecomResult()
        recom_result.ppuid = '0'
        recom_result.reason_id = '006'
        recom_result.reason_desc = 'common'
        self.res.sort(self.tuple_cmp)
        for e in self.res:
            recom_atom = class_set.RecomAtom()
            recom_atom.item_id = e[0]
            recom_atom.involved_num = e[1]
            recom_result.resys_items.append(recom_atom)
        print recom_result

    def processor(self, key, lst):
        count = 0
        for e in lst:
            count += int(e)
        if count >= 0:
            self.res.append((key, count))

def main():
    cr = CommonRecommendationReducer()
    cr.reducer()
    cr.summary()

if __name__ == '__main__':
    main()

